unit uMain;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, 
  FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
  FMX.Layouts, FMX.Memo, FMX.Edit, Xml.xmldom, Xml.XMLIntf, Xml.Win.msxmldom,
  Xml.XMLDoc;

type
  TfrmPACtoSuperPutty = class(TForm)
    txtPACFile: TEdit;
    lblPACFile: TLabel;
    btnOpen: TButton;
    lblReplaceFrom: TLabel;
    lblReplaceTo: TLabel;
    mReplaceFrom: TMemo;
    mReplaceTo: TMemo;
    btnConvert: TButton;
    dlgSave: TSaveDialog;
    dlgOpen: TOpenDialog;
    XMLDocument1: TXMLDocument;
    procedure btnConvertClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnOpenClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmPACtoSuperPutty: TfrmPACtoSuperPutty;

implementation

{$R *.fmx}

procedure TfrmPACtoSuperPutty.btnConvertClick(Sender: TObject);
type
  TSSHConnection = record
    strName : String;
    strHost : String;
    strPort : String;
    strProto : String;
  end; // TSSHConnection

  procedure AddSession(SSHConnection : TSSHConnection);
  begin
    with XMLDocument1.DocumentElement.AddChild('SessionData'), SSHConnection do
    begin
      if (strHost = '') or (strPort = '') then Exit;

      Attributes['SessionName'] :=strName;
      Attributes['Host'] :=strHost;
      Attributes['Port'] :=strPort;
      Attributes['Proto'] :=strProto;
    end; // with
  end;

var
  fPACFile : TStringList;
  iLine : Integer;
  strLine : String;
  strValue : TArray<String>;
  SSHConnection : TSSHConnection;
  strFileName : String;
begin
  if not dlgSave.Execute then Exit;

  XMLDocument1.DocumentElement.ChildNodes.Clear;

  fPACFile :=TStringList.Create;
  fPACFile.LoadFromFile(dlgOpen.FileName);

  for iLine :=0 to mReplaceFrom.Lines.Count - 1 do
    fPACFile.Text :=StringReplace(fPACFile.Text, mReplaceFrom.Lines[iLine], mReplaceTo.Lines[iLine], [rfReplaceAll, rfIgnoreCase]);

  for iLine :=0 to fPACFile.Count - 1 do
  begin
    strLine :=fPACFile.Strings[iLine];

    if strLine.Substring(0, 2) = '  ' then
    begin
      strLine :=strLine.Trim;
      strValue :=strLine.Split([':']);

      if Length(strValue) > 1 then
      begin
        strValue[0] :=strValue[0].Trim;
        strValue[1] :=strValue[1].Trim;
      end; // if

      if SameText(strValue[0], 'ip') then
        SSHConnection.strHost :=strValue[1];

      if SameText(strValue[0], 'port') then
        SSHConnection.strPort :=strValue[1];

      if SameText(strValue[0], 'name') then
        SSHConnection.strName :=strValue[1];

      if SameText(strValue[0], 'method') then
        SSHConnection.strProto :=strValue[1];
    end
    else
    begin
      if (strLine.Chars[strLine.Length - 1] = ':') then
      begin
        AddSession(SSHConnection);
        FillChar(SSHConnection, SizeOf(SSHConnection), 0);
      end; // if
    end; // if
  end; // for

  AddSession(SSHConnection);

  strFileName :=ChangeFileExt(dlgSave.FileName, '.xml');
  XMLDocument1.SaveToFile(strFileName);
  fPACFile.Free;


  ShowMessage('Successfully exported to: ' + strFileName);
end;


procedure TfrmPACtoSuperPutty.btnOpenClick(Sender: TObject);
begin
  if not dlgOpen.Execute then Exit;

  txtPACFile.Text :=dlgOpen.FileName;
end;

procedure TfrmPACtoSuperPutty.FormCreate(Sender: TObject);
begin
  XMLDocument1.AddChild('ArrayOfSessionData');
end;

end.
